package cc.catface.jpa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserDao userDao;

    /*Repository接口测试*/
    @GetMapping("findAll")
    public List<User> findAll() {
        return userDao.findAll();
    }

    @GetMapping("findByUsername")
    public List<User> findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @GetMapping("findByUsernameLike")
    public List<User> findByUsernameLike(String username) {
        return userDao.findByUsernameLike("%" + username + "%");
    }

    @GetMapping("findByUsernameLikeAndUserageGreaterThanEqual")
    public List<User> findByUsernameLikeAndUserageGreaterThanEqual(String username, Integer userage) {
        return userDao.findByUsernameLikeAndUserageGreaterThanEqual("%" + username + "%", userage);
    }


    /*@Query接口测试*/
    @GetMapping("queryUserByUsername")
    public List<User> queryUserByUsername(String username) {
        return userDao.queryUserByUsername(username);
    }

    @GetMapping("queryUserByUsernameLike")
    public List<User> queryUserByUsernameLike(String username) {
        return userDao.queryUserByUsernameLike(username);
    }

    @GetMapping("queryUserByUsernameLikeAndUserage")
    public List<User> queryUserByUsernameLikeAndUserage(String username, Integer userage) {
        return userDao.queryUserByUsernameLikeAndUserage(username, userage);
    }

    /*sql查询*/
    @GetMapping("selectUserByUsernameLikeAndUserage")
    public List<User> selectUserByUsernameLikeAndUserage(String username, Integer userage) {
        return userDao.selectUserByUsernameLikeAndUserage(username, userage);
    }


    /*@Query更新数据*/
    @GetMapping("updateUserageById")
    public void updateUserageById(Integer id, Integer userage) {
        userDao.updateUserageById(id, userage);
    }

    @GetMapping("save")
    public void save(String username, int age) {
        User user = new User();
        user.setUsername(username);
        user.setUserage(age);
        userDao.save(user);

    }
}
